package com.shopping.cart.dao;

import com.alibaba.fastjson.JSON;
import com.shopping.cart.entity.Product;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qxf101
 * @since 2021-05-12
 */
public class LoadProductInformation {
    public static List<Product> loadDataFromJson() {
        try {
            InputStream inputStream = LoadProductInformation.class.getClassLoader().getResourceAsStream("product_info.json");
            if (inputStream != null) {
                return JSON.parseArray(IOUtils.toString(inputStream, StandardCharsets.UTF_8), Product.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ArrayList<>();
    }
}
